package webservice;

import java.io.IOException;
import java.io.InputStreamReader;
import java.rmi.ServerException;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

import net.rim.device.api.system.Application;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.Screen;
import net.rim.device.api.ui.Ui;
import net.rim.device.api.ui.UiEngine;
import net.rim.device.api.ui.component.Dialog;
import test.ConnectionCreator;

public class WebServiceCaller {

	public WebServiceCaller(){}

	// send user and password, get login
	public String getLoginHash(String user, String pass){
		String hash=readFromWs(WebServiceURLs.LOGIN+user+"/"+pass);
		return hash;
	}

	// send login hash, get info
	public String getXmlFromWs(String hash){
		System.out.println(WebServiceURLs.NOTAS+hash);
		String xml= readFromWs(WebServiceURLs.NOTAS+hash);
		return xml;
	}

	public String getPeriodosClaseXml(String hash){
		System.out.println(WebServiceURLs.NOTAS+hash);
		return readFromWs(WebServiceURLs.NOTAS+hash);
	}
	public String getMoreInfoClaseXml(String hash, String aulaID){
		System.out.println(WebServiceURLs.NOVEDADESYARCHIVOS+hash+"/"+aulaID);
		return readFromWs(WebServiceURLs.NOVEDADESYARCHIVOS+hash+"/"+aulaID);
	}
	public String getGrupoXml(String hash, String aulaID){
		System.out.println(WebServiceURLs.GRUPO+hash+"/"+aulaID);
		return readFromWs(WebServiceURLs.GRUPO+hash+"/"+aulaID);
	}

	public String sendCommentInGroup(String url){
		return readFromWs(url);
	}

	public String suscribirPush(String hash, String pin) {
		//SuscribirPush/{hash}/{TipoDispositivo}/{IdDispositivo}/{AppId}?format={format}
		System.out.println(WebServiceURLs.PUSH+hash+WebServiceURLs.PUSH_TYPE+
				pin+WebServiceURLs.PUSH_APPID+WebServiceURLs.PUSH_FORMAT);
		return readFromWs(WebServiceURLs.PUSH+hash+WebServiceURLs.PUSH_TYPE+
				pin+WebServiceURLs.PUSH_APPID+WebServiceURLs.PUSH_FORMAT);
	}

	public String desSuscribirPush(String hash, String pin) {
		//DesSuscribirPush/{hash}/{TipoDispositivo}/{IdDispositivo}/{AppId}?format={format}
		System.out.println(WebServiceURLs.DES_PUSH+hash+WebServiceURLs.PUSH_TYPE+
				pin+WebServiceURLs.PUSH_APPID+WebServiceURLs.PUSH_FORMAT);
		return readFromWs(WebServiceURLs.DES_PUSH+hash+"/"+//WebServiceURLs.PUSH_TYPE+
				pin+WebServiceURLs.PUSH_APPID+WebServiceURLs.PUSH_FORMAT);
	}

	public String changePushStatus(String hash, String pin, int status) {
		System.out.println(WebServiceURLs.PUSH_STATUS+hash+WebServiceURLs.PUSH_TYPE+
				pin+WebServiceURLs.PUSH_APPID+"/"+status+WebServiceURLs.PUSH_FORMAT);
		return readFromWs(WebServiceURLs.PUSH_STATUS+hash+WebServiceURLs.PUSH_TYPE+
				pin+WebServiceURLs.PUSH_APPID+"/"+status+WebServiceURLs.PUSH_FORMAT);
	}

	public String getCompatibleVersion(String version) {
		System.out.println("lalalaaaaaaaaaaaaaa - " + WebServiceURLs.VERSION+version+"/"+WebServiceURLs.APP_ID+WebServiceURLs.FORMAT_JSON);
		return readFromWs(WebServiceURLs.VERSION+version+"/"+WebServiceURLs.APP_ID+WebServiceURLs.FORMAT_JSON);
	}

	public String readFromWs(String url){

		String html="";
		String params="";

		try {
			params = ConnectionCreator.getConnectionString();
		} catch (ServerException e) {
			e.printStackTrace();
		}

		try {
			System.out.println("*********************************"+url+params);
			HttpConnection conn = (HttpConnection) Connector.open(url+params);
			StringBuffer raw = new StringBuffer();
			if (conn.getResponseCode() == HttpConnection.HTTP_OK) {
				java.io.Reader r = new InputStreamReader(conn.openInputStream());
				int ch;
				while ((ch = r.read()) != -1) {
					raw.append((char) ch);
				}
				html = raw.toString();
			}
			conn.close();
		} catch (IOException e) {
			e.printStackTrace();
			log.Logger.log("HttpConnection exception: could not connect to webservice");
		}
		return html;	
	}


}
